package com.niit.onlinemusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niit.onlinemusic.common.JwtUtils;
import com.niit.onlinemusic.common.R;
import com.niit.onlinemusic.model.domain.Consumer;
import com.niit.onlinemusic.model.domain.Token;
import com.niit.onlinemusic.model.request.ConsumerRequest;
import com.niit.onlinemusic.service.ConsumerService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * 用户控制器
 *
 * @author zuwei522
 * @since 2025/9/17 08:41
 */
@RequestMapping
@RestController
public class ConsumerController {

    private final ConsumerService consumerService;

    public ConsumerController(ConsumerService consumerService) {
        this.consumerService = consumerService;
    }

    @GetMapping("user")
    public R allUser(@RequestParam(defaultValue = "1") Integer curPage,
                     @RequestParam(defaultValue = "5") Integer pageSize) {
        IPage<Consumer> page = new Page<>(curPage, pageSize);
        return consumerService.allUser(page);
    }

    @GetMapping("users")
    public R allUsers() {
        return R.success("查询成功", consumerService.list());
    }

    /**
     * 删除单个用户
     *
     * @param id 用户id
     * @return 删除结果
     * @author zuwei522
     * @since 2025/9/19 09:34
     */
    @GetMapping("user/delete")
    public R deleteUser(@RequestParam("id") Integer id) {
        if (consumerService.removeById(id)) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 删除多个用户
     *
     * @param ids 用户id数组
     * @return 删除结果
     * @author zuwei522
     * @since 2025/9/19 08:54
     */
    @DeleteMapping("user/deleteIds")
    public R deleteUsers(@RequestParam("id") Integer[] ids) {
        if (consumerService.removeBatchByIds(Arrays.asList(ids))) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @PostMapping("user/login/status")
    public R loginStatus(@RequestBody ConsumerRequest consumerRequest, HttpSession session) {
        R result = consumerService.loginStatus(consumerRequest, session);
        if (result.getSuccess()) {
            String tokenStr = JwtUtils.generateToken(consumerRequest.getUsername());
            Token token = new Token();
            token.setAuthorization(tokenStr);
            token.setObj(result.getData());
            result.setData(token);
        }
        return result;
    }

    /**
     * 更新头像
     *
     * @param multipartFile 文件的二进制流
     * @param id            用户主键id
     * @return 更新结果
     * @author zuwei522
     * @since 2025/9/29 02:26
     */
    @PostMapping("user/avatar/update")
    public R updateAvatar(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") Long id) {
        return consumerService.updateAvatar(multipartFile, id);
    }

}
